from PIL import Image

import os
import sys


def convert_image(img_path, new_ext):
    name, _ = os.path.splitext(img_path)
    new_path = name + "." + new_ext
    
    print("convert {} to {}".format(img_path, new_path))
    
    img = Image.open(img_path)
    img.load()
    img.save(new_path)


if __name__ == "__main__":
    if len(sys.argv) < 3:
        print("Usage: {} new_ext file ... ".format(sys.argv[0]))
        print("")
        print("eg:    {} png 1.png 2.webp".format(sys.argv[0]))
        sys.exit(0)
        
    new_ext = sys.argv[1]
    for img in sys.argv[2:]:
       convert_image(img, new_ext)
        

